package com.rico.kok.controller.user;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rico.kok.entity.User;
import com.rico.kok.service.interf.FollowService;
import com.rico.kok.service.interf.UserService;
import com.rico.kok.tool.MessageUtils;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class FollowController {

    @Autowired
    FollowService followService;

    @Autowired
    UserService userService;

    @Autowired
    MessageUtils messageUtils;

    //关注和取关用户

    /**
     * 已经关注返回true值，取消关注返回false
     *
     * @param sFollowId
     * @param session
     * @return
     */
    @RequiresRoles("user")
    @RequestMapping("/followUser.htm")
    @ResponseBody
    public String followUser(@RequestParam("id") String sFollowId, HttpSession session,Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            throw new AuthorizationException();
        }
        int userId = user.getUserId();
        int followId;
        try {
            followId = Integer.parseInt(sFollowId);
        }catch (Exception e){
            System.out.println("不合法ID");
            messageUtils.setMsg(model, "错误", "不合法ID", "error");
            return "msg";
        }
        List<Integer> list = followService.selectAllFollowsId(userId);
        int index = list.indexOf(followId);
        if (index == -1) {
            int res = followService.followUser(userId, followId);
            if (res == 1) {
                return "true";
            } else {
                return "false";
            }
        } else {
            followService.unFollowUser(user.getUserId(), followId);
            return "false";
        }
    }

    //获取关注列表
    @RequiresRoles("user")
    @RequestMapping("/followList.htm")
    public String followList(@RequestParam(value = "num",defaultValue = "1") Integer pageNum,HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        List<Integer> ids = followService.selectAllFollowsId(user.getUserId());
        if (ids == null || ids.size() < 1) {
            model.addAttribute("follows", new ArrayList<User>());
            return "/follow_list";
        }
        PageHelper.startPage(pageNum,6);
        List<User> followUsers = userService.selectUsersByIds(ids);
        if (followUsers == null || followUsers.size() < 1) {
            followUsers = new ArrayList<User>();
        }
        PageInfo<User> pageInfo=new PageInfo<>(followUsers);
        String url="/followList.htm?num="+pageNum;
        model.addAttribute("pageInfo", pageInfo);
        return "/follow_list";
    }
}
